<template>
	<el-container>
		<el-aside width="200px" v-loading="showGrouploading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="group" class="menu" node-key="deptId" :data="group" :props="groupsProps" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header style="height: auto;">
				<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" stripe remoteSort remoteFilter>
					<el-table-column label="#" type="index" width="50"></el-table-column>
					<el-table-column label="报销人" prop="accountId" width="100" sortable='custom'>
						<template #default="scope">
							{{scope.row.userName}}
						</template>
					</el-table-column>
					<el-table-column label="所在部门" prop="deptName" width="250" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="年度" prop="createTime" width="150"></el-table-column>
					<el-table-column label="报销金额" prop="amount" width="200">
						<template #default="scope">
							{{scope.row.amount}}/元
						</template>
					</el-table-column>
					<el-table-column label="报销流程" prop="flowTitle" width="300" :show-overflow-tooltip="true">
						<template #default="scope">
							<span style="cursor: pointer;color: #409EFF" @click="readBpmDetails(scope.row, scope.$index)">{{scope.row.flowTitle}}</span>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
</template>

<script>
import scSelectFilter from '@/components/scSelectFilter'
export default {
	name: 'Expense Account Manage',
	components: {
		scSelectFilter,
	},
	data() {
		return {
			filterData: [
				{
					title: "报销年度",
					key: "expenseYear",
					multiple: false,
					options: [{
						label: "全部",
						value: ""
					},]
				},
				{
					title: "费用类型",
					key: "expenseType",
					multiple: false,
					options: [{
						label: "全部",
						value: ""
					},]
				}
			],
			showGrouploading: false,
			groupFilterText: '',
			group: [],
			groupsProps: {
				label:"name",
				value: "id"
			},
			apiObj: this.$API.finance.expenseClaim.getExpenseClaimList,
			search: {
				expenseYear:'',
				expenseType:'',
				deptId:'',
				accountId:'',
			}
		}
	},
	watch: {
		groupFilterText(val) {
			this.$refs.group.filter(val);
		}
	},
	mounted() {
		this.getDeptGroup();
		this.getExpenseYear();
		this.getExpenseAccountForSelect();
	},
	methods: {
		async getExpenseAccountForSelect(){
			var res = await this.$API.finance.expense.getExpenseAccountForSelect.get();
			if (res.code == 200) {
				this.filterData[1].options=this.filterData[1].options.concat(res.data);
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		getExpenseYear()
		{
			let yearArr=[];
			let currentYear = parseInt(new Date().getFullYear());
			for(let i=0;i<10;i++)
			{
				let year = currentYear-i;
				let tempJson={
					value:year,
					label:year+"年度"
				};
				yearArr.push(tempJson);
			}
			this.filterData[0].options=this.filterData[0].options.concat(yearArr);
		},
		//加载树数据
		async getDeptGroup(){
			this.showGrouploading = true;
			var res = await this.$API.system.user.getAllUserForTree.get();
			this.showGrouploading = false;
			var allNode ={id: '', name: '所有'}
			res.data.unshift(allNode);
			this.group = res.data;
		},
		//树过滤
		groupFilterNode(value, data){
			if (!value) return true;
			return data.name.indexOf(value) !== -1;
		},
		//树点击事件
		groupClick(data){
			if(data.id=='')
			{
				this.search.deptId="";
				this.search.accountId = "";
			}else{
				if(data.isUser)
				{
					this.search.accountId = data.id
					this.search.deptId="";
				}else{
					this.search.deptId = data.id;
					this.search.accountId = "";
				}
			}
			this.$refs.table.upData(this.search)
		},
		filterChange(data){
			this.search.expenseYear = data.expenseYear;
			this.search.expenseType = data.expenseType;
			this.$refs.table.upData(this.search)
		},
		//搜索
		upsearch(){
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSaveSuccess(){
			this.$refs.table.refresh()
		},
		readBpmDetails(row,index)
		{
			let url='/bpm/handle/read';
			if(row.model=='1')
			{
				url='/doc/handle/read';
			}
			this.$nextTick(() => {
				this.$router.replace({
					path: url,
					query: {
						runId: row.runId
					}
				})
			})
		},
	}
}
</script>

<style>
</style>
